﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;
using TuanAnhPersonalWeb.Classes;
using MvcPaging;

namespace TuanAnhPersonalWeb.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin")]
    public class RolesController : Controller
    {
        private RolesAccessConsolidator access = new RolesAccessConsolidator();
        private const int defaultPageSize = 10;
        public ActionResult Index(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var list = access.GetAllRoles().OrderBy(e => e.RoleName).ToList().ToPagedList(currentPageIndex, defaultPageSize);

            if (Request.IsAjaxRequest())
            {

                return PartialView("_ListControl", list);
            }
            else
                return View(list);
        }
        //
        // SEARCH

        public ActionResult Search(int? page, string valSearch = "")
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            ViewData["valSearch"] = valSearch;
            var list = access.GetAllRoles();
            list = list.Where(e => e.LoweredRoleName.Contains(valSearch.ToLower())).OrderBy(e => e.RoleName).ToList().ToPagedList(currentPageIndex, defaultPageSize);
            return PartialView("_ListControl", list);
        }
        //
        // GET: /Roles/Details/5

        public ActionResult Details(string id)
        {
            aspnet_Roles roles = access.GetRolesById(System.Guid.Parse(id));
            return View(roles);
        }

        //
        // GET: /Roles/Create

        public ActionResult Create()
        {
            return View(new aspnet_Roles());
        }

        //
        // POST: /Roles/Create

        [HttpPost]
        public ActionResult Create(aspnet_Roles roles)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View("Create", roles);
                }
                else
                {
                    access.CreateNew(roles);
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Roles/Edit/5

        public ActionResult Edit(string id)
        {
            aspnet_Roles roles = access.GetRolesById(System.Guid.Parse(id));
            return View(roles);
        }

        //
        // POST: /Roles/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, aspnet_Roles roles)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View("Edit", roles);
                }
                roles = access.GetRolesById(System.Guid.Parse(id));
                if (roles != null)
                {

                    UpdateModel(roles);
                    roles.LoweredRoleName = roles.RoleName.ToLower();
                    access.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    throw new NoRecordException();
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Roles/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Roles/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
